<template>
  <div class="search-history">
    <van-cell title="搜索历史">
      <span v-show="isDeleteShow" @click="$emit('clear-search-history', [])"
        >全部删除</span
      >
      &nbsp;
      <span v-show="isDeleteShow" @click="isDeleteShow = false">完成</span>
      <van-icon
        name="delete"
        v-show="!isDeleteShow"
        @click="isDeleteShow = true"
      ></van-icon>
    </van-cell>
    <van-cell
      v-for="(text, index) in searchHistory"
      :key="index"
      :title="text"
      @click="onSearchItemClick(text, index)"
    >
      <van-icon name="close" v-if="isDeleteShow"></van-icon>
    </van-cell>
  </div>
</template>

<script>
export default {
  name: 'SearchHistory',
  components: {},
  props: {
    searchHistory: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      isDeleteShow: false
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    onSearchItemClick(item, i) {
      if (this.isDeleteShow) {
        // 删除历史记录
        this.searchHistory.splice(i, 1)
      } else {
        this.$emit('search', item)
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
